package com.tripcube.auth.controller;

import com.tripcube.auth.service.SocialLoginService;
import com.tripcube.common.core.domain.R;
import com.tripcube.common.security.service.TokenService;
import com.tripcube.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 第三方登录控制器
 *
 * @author yourname
 */
@RestController
@RequestMapping("/auth/social")
public class SocialLoginController {

    @Autowired
    private SocialLoginService socialLoginService;

    @Autowired
    private TokenService tokenService;

    /**
     * GitHub登录回调
     */
    @GetMapping("/github/callback")
    public R<?> githubCallback(@RequestParam String code, @RequestParam String state) {
        try {
            // 处理GitHub登录
            LoginUser loginUser = socialLoginService.handleGitHubLogin(code);
            Map<String, Object> result = new HashMap<>();
            result.put("token", tokenService.createToken(loginUser));
            return R.ok(result);
        } catch (Exception e) {
            return R.fail("GitHub登录失败: " + e.getMessage());
        }
    }

    /**
     * QQ登录回调
     */
    @GetMapping("/qq/callback")
    public R<?> qqCallback(@RequestParam String code, @RequestParam String state) {
        try {
            // 这里应该调用QQ API获取用户信息
            // 为简化示例，我们假设已经获取了用户信息
            String username = "qq_" + code; // 实际应该从QQ API获取
            String email = username + "@qq.com"; // 实际应该从QQ API获取

            // 处理用户登录或注册
            LoginUser loginUser = socialLoginService.handleSocialUser(username, username, email, "qq");
            Map<String, Object> result = new HashMap<>();
            result.put("token", tokenService.createToken(loginUser));
            return R.ok(result);
        } catch (Exception e) {
            return R.fail("QQ登录失败: " + e.getMessage());
        }
    }

    /**
     * 微信登录回调
     */
    @GetMapping("/wechat/callback")
    public R<?> wechatCallback(@RequestParam String code, @RequestParam String state) {
        try {
            // 这里应该调用微信API获取用户信息
            // 为简化示例，我们假设已经获取了用户信息
            String username = "wechat_" + code; // 实际应该从微信API获取
            String email = username + "@wechat.com"; // 实际应该从微信API获取

            // 处理用户登录或注册
            LoginUser loginUser = socialLoginService.handleSocialUser(username, username, email, "wechat");
            Map<String, Object> result = new HashMap<>();
            result.put("token", tokenService.createToken(loginUser));
            return R.ok(result);
        } catch (Exception e) {
            return R.fail("微信登录失败: " + e.getMessage());
        }
    }

    /**
     * 处理第三方用户登录
     * @param username 用户名
     * @param email 邮箱
     * @param source 来源
     * @return LoginUser
     */
    private LoginUser handleSocialLogin(String username, String email, String source) {
        return socialLoginService.handleSocialUser(username, username, email, source);
    }
}